<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>角色授权</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
 
<body class="iframeBody">
<div class="Wrapper">
    <table class="layout2col1">
        <tbody>
            <tr>
                <td style="width: 500px;" valign="top">
                    <div  class="sqPanel">
                        <div class="sqPanel_head">
                            <form id="searchBar1" class="titSearchBar">
                                <div class="fieldset1 clearFix">
                                    <div class="fieldBox">
                                        <div class="label">角色</div>
                                        <div class="field">
                                             <input name="roleNmS" id="roleNmS" type="text" class="ui-textbox" value="" ligerui="width: 100">
                                        </div>
                                    </div>
                                    <span id="searchBtn1" class="sq-search-button"></i>查询</span>
                                </div>
                            </form>
                        </div>
                        <div class="sqPanel_body">
                            <div class="FBL5">
                                <span id="addBtn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_3"></i>新增</span>
                                <span id="changeBtn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_4"></i>修改</span>
                                <span id="inheritBtn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_16"></i>权限继承</span>
                            </div>
                            <div id="ligergrid_roles" class="sq-grid"></div>
                        </div>
                    </div>
                </td>
                <td style="width: 300px;" valign="top">
                    <div class="sqPanel">
                        <div class="sqPanel_head">
                            <form id="searchBar2" class="titSearchBar">
                                <div class="fieldset1 clearFix">
                                    <div class="fieldBox">
                                        <div class="label">资源名称</div>
                                            <div class="field">
                                                 <input name="resNm" id="resNm" type="text" class="ui-textbox" value="" ligerui="width: 100">
                                            </div>
                                        </div>
                                    <span id="searchBtn2" class="sq-search-button"></i>查询</span>
                                </div>
                            </form>
                        </div>
                        <div class="sqPanel_body" style="min-height: 300px;">
                            <div class="FBL5">
                                <span id="authoBtn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_30"></i>授权</span>
                            </div>
                            <div class="nanoWrap" style="height: 320px;">
                                <div class="nano"><div class="nano-content">
                                <ul id="tree1">
                                </ul>
                                </div></div>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<div id="addUsr_Html" style="display:none">
    <form id="addUsr_form">
         <input name="idS" id="idS" type="text" style="display: none;" value=""/>
        <div class="fieldset1 tabel_col clearFix">
            
            <div class="fieldBox col_1_100">
                <div class="label">角色名称</div>
                <div class="field">
                    <input name="roleNm" id="roleNm" type="text" class="ui-textbox" value="" validate="{required:true}">
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
           
            
            <div class="fieldBox col_1_100">
                <div class="label">角色类别</div>
                <div class="field">
                    <select name="roleTypCd" id="roleTypCd" validate="{required:true, select: true}">
                        <option value=" " seleted>请选择</option>
                        <c:forEach items="${roleTypCdList }" var="roleTyp">
                            <option value="${roleTyp.key }">${roleTyp.value }</option>
                        </c:forEach>
                    </select>
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
            <div class="fieldBox col_1_100">
                <div class="label">是否显示</div>
                <div class="field">
                    <select name="isVis" id="isVis" validate="{required:true, select: true}">
                        <c:forEach items="${isVisCdList }" var="item" varStatus="status">
                        	<option value="${item.key }">${item.value }</option>
                        </c:forEach>
                    </select>
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
            <div class="fieldBox col_1_100">
                <div class="label">角色描述</div>
                <div class="field" style="width:100%">
                     <textarea name="roleRem" id="roleRem" rows="2" class="l-textarea" style="width:100%"></textarea>
                </div>
            </div>
            
            
            
        </div>
        <div class="btnsBox">
            <span id="saveNewUsr_Btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_10"></i>保存</span>
            <span id="changeSave_Btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_10"></i>修改</span>
            <span class="sq-button sq-button-hasicon l-dialog-btn-cancel"><i class="sqBtnIco_14"></i>取消</span>
            <input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
        </div>
    </form>
</div>

<div id="addRoleInherit_Html" style="display:none">
            <form id="searchBar_roleInherit" class="l-form">
                <div class="fieldset1 clearFix">
                    <div class="FBL5" style="padding: 0;">
                        <div class="fieldBox">
                            <div class="label">角色</div>
                            <div class="field">
                                 <input name="jcRoleNm" id="jcRoleNm" type="text" class="ui-textbox" value="" ligerui="width: 100">
                            </div>
                        </div>
                        <span id="searchBtn_roleInherit" class="sq-button sq-button-hasicon"><i class="sqBtnIco_1"></i>查询</span>
                    </div>
                </div>
            </form>
            <div id="ligergrid_inheritRoles" class="sq-grid"></div>
            <div class="btnsBox TC">
                <span class="sq-button sq-button-hasicon l-dialog-btn-ok"><i class="sqBtnIco_30"></i>确认继承</span>
                <span class="sq-button sq-button-hasicon l-dialog-btn-cancel"><i class="sqBtnIco_14"></i>取消</span>
            </div>
</div>


<script type="text/javascript">
var grid,
    tree,
    grid_inheriRoles=null,
    RESOURCE_IDs=[],
    Editable=true;
$(function () {
    $("#searchBar1").ligerForm();
    $("#searchBtn1").click(function() {
        var form = liger.get("searchBar1");
        var data = form.getData();
        //alert(JSON.stringify(data));
        for (name in data){
        grid.setParm(name,data[name]);
        };
        grid.reload();
    });
    $("#inheritBtn").click(function() {
        if (grid.getSelected()==null) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE,SQ_type: "warning"});
            return;
        };
        $.ligerDialog.SQopen({
            width: 600,
            top: 25,
            title : '权限继承',
            target: $("#addRoleInherit_Html")
        });
        $("#searchBar_roleInherit").ligerForm();
        var form = liger.get("searchBar_roleInherit");
        var data = form.getData();
        //for (name in data){
          grid_inheriRoles.setParm("roleNmS",data["jcRoleNm"]);
        //};
        grid_inheriRoles.reload();
        //用完把参数置为空,主要是为了在没有选择数据后关闭窗口,直接点击下面的grid分页会带入这个参数查询。
        grid_inheriRoles.setParm("roleNmS","");
       
    });
    $("#searchBtn_roleInherit").click(function() {
        var form = liger.get("searchBar_roleInherit");
        var data = form.getData();
        //alert(JSON.stringify(data));
        //for (name in data){
        	grid_inheriRoles.setParm("roleNmS",data["jcRoleNm"]);
        //};
        grid_inheriRoles.reload();
        //用完把参数置为空,主要是为了在没有选择数据后关闭窗口,直接点击下面的grid分页会带入这个参数查询。
        grid_inheriRoles.setParm("roleNmS","");
    });
    $("#addRoleInherit_Html .l-dialog-btn-ok").click(function() {
        if (grid_inheriRoles.getSelected()==null) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_EXTEND_ROLE,SQ_type: "warning"});
            return;
        };
        
        curRoleId=grid.getSelected().id;
        extRoleId=grid_inheriRoles.getSelected().id;
        if (curRoleId==extRoleId) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_NOT_SELECT_SAME_ROLE,SQ_type: "warning"});
            return;
        };
        $.ligerDialog.hide();
        $.ajax({
        	url: '<%=path%>/system/sysRoleResR/extendResR.do',
            type: 'post',
            dataType: 'json',
            data:{'curRoleId':curRoleId,'extRoleId':extRoleId},
            success:function(msg){
				 if(msg.success == 1){
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
					 resetResource (curRoleId);
				 }else{
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
				 }
			}
        });
       // ligerDialogSuc("继承成功！", 1000);
    });
    $("#searchBar2").ligerForm();
    $("#searchBtn2").click(function() {
        var form = liger.get("searchBar2");
        var data = form.getData();
        $.extend(true, data, { RESOURCE_IDs: RESOURCE_IDs});
        //alert(JSON.stringify(data));
        
        searchResource(data);
    });
    $("#authoBtn").click(function() {
        if (!grid.getSelectedRow()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE,SQ_type: "warning"});
            return;
        };
       
        resIdArr=RESOURCE_IDs;
        //alert(resIdArr);
        roleId=grid.getSelectedRow().id;
        
        $.ajax({
        	url: '<%=path%>/system/sysRoleResR/add.do',
            type: 'post',
            dataType: 'json',
            data:{'roleId':roleId,'resIdArr[]':resIdArr},
            success:function(msg){
				 if(msg.success == 1){
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
				 }else{
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
				 }
			}
        });
        //ligerDialogSuc("授权成功！",1000)
    });
    grid = $("#ligergrid_roles").ligerGrid({
        url: '<%=path%>/system/sysRole/initList.do',
        method : 'get',
        root:'rows',
        record:'total',
        fixedCellHeight: false,
        // checkbox: true,
        // frozenCheckbox: false,
        // isSingleCheck: true,
        allowHideColumn: false,
        rownumbers:true,
        columns: [{
            display: '角色名称',
            name: 'roleNm',
            minWidth: 120,
            render: function (rowdata, rowindex, value){
        		return UIUtils.showColumnTip(value);
            }
        }, {
            display: '状态',
            name: 'isValidNm',
            minWidth: 60,
            render: function (rowdata, rowindex, value) {
            	if(rowdata.isValid == CommonConstant.C_YN_Y_1){
            		return LogUtils.LOG_CMM_GRID_BTN_ENABLE;
            	}else if(rowdata.isValid == CommonConstant.C_YN_N_0){
            		return LogUtils.LOG_CMM_GRID_BTN_DISABLE;
            	}
            }
        }, {
            display: '是否显示',
            name: 'isVisNm',
            minWidth: 60,
            render: function (rowdata, rowindex, value) {
            	if(rowdata.isVis == 1){
            		return CommonConstant.C_YN_Y_NM;
            	}else if(rowdata.isVis == 0){
            		return CommonConstant.C_YN_N_NM;
            	}
            }
        }, {
            display: '角色类别',
            name: 'roleTypCdNm',
            minWidth: 60,
            render: function (rowdata, rowindex, value){
        		return UIUtils.showColumnTip(value);
            }
        }, {
            display: '备注',
            name: 'roleRem',
            minWidth: 150,
            render: function (rowdata, rowindex, value){
        		return UIUtils.showColumnTip(value);
            }
        }, {
            display: '操作',
            minWidth: 80,
            render: function (rowdata, rowindex, value) {
                var h;
                var h1='<a class="sq-button-hasicon" href="javascript:;" onclick="discardRole(event';
                    h1+=",'"+rowdata.id+"'";
                    h1+=",'"+rowdata.isSysDef+"'";
                    h1+=",'ligergrid_roles'";
                    h1+=')"><i class="sqBtnIco_13"></i>禁用</a>';
                var h2='<a class="sq-button-hasicon" href="javascript:;" onclick="enableRole(event';
                    h2+=",'"+rowdata.id+"'";
                    h2+=",'"+rowdata.isSysDef+"'";
                    h2+=",'ligergrid_roles'";
                    h2+=')"><i class="sqBtnIco_12"></i>启用</a>';
                switch (rowdata.isValid){
                    case CommonConstant.C_YN_Y_0:
                        h=h2
                        break;
                    case CommonConstant.C_YN_Y_1:
                        h=h1
                        break;
                    default:
                        h='';
                }
                return h;
            }
        }],
        width: '100%',
        //height:'100%',
        heightDiff:-2,
        // rownumbers: true,
        onSelectRow: function (rowdata, rowid, rowobj) {
            //alert("id:"+rowdata.id);
            resetResource(rowdata.id);
            isEditable(rowdata.isSysDef);
        },
        onAfterShowData: function() {
            SetIframe_height();
        }
    });
    
    //grid.setHeight(100);
    
    $(".nanoWrap .nano").nanoScroller({
        preventPageScrolling: true
    });
    tree = $("#tree1").ligerTree({
    	url: '<%=path%>/system/sysRoleResR/getTreeByRoleId.do',
        ajaxType: 'get',
        nodeWidth: 300,
        parentIcon: "SqFolder",
        childIcon: "SqLeaf",
        iconClsFieldName: "iconClass",
        enabledCompleteCheckbox: false,
        onCheck: function (item,checked) {
            if (!grid.getSelectedRow()) {
                top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE,SQ_type: "warning"});
                tree.cancelSelect(item);
                return;
            };
            //var checkNodes=getCheckNodes(item.data);
            //setRESOURCE_IDs(checkNodes, checked);
            SQ.setIDs_onCheckTree(item, checked, tree, RESOURCE_IDs);
            //alert(RESOURCE_IDs);
        },
        onSuccess: function (data) {
            setNanoScroller();
        }
    });
    function setRESOURCE_IDs (checkNodes, ischecked) {
        if (ischecked) {
            for (var i = 0; i < checkNodes.length; i++) {
                var checkedID=checkNodes[i].id;
                if(RESOURCE_IDs.indexOf(checkedID)==-1) RESOURCE_IDs.push(checkedID);
            };
        } else{
            for (var i = 0; i < checkNodes.length; i++) {
                var checkedID=checkNodes[i].id;
                var index=RESOURCE_IDs.indexOf(checkedID);
                if(index>=0) RESOURCE_IDs.splice(index,1);
            };
        };
    }
    // 获取tree check或uncheck的时候,触发的所有node，包括子节点
    function getCheckNodes (data) {
        var checkNodes=[];
        function findNodes (node) {
            checkNodes.push(node);
            if (tree.hasChildren(node)) {
                for (var i = 0; i < node.children.length; i++) {
                    var childrenNode=node.children[i];
                    findNodes (childrenNode);
                };
            }else{
                return;
            };
        }
        findNodes (data);
        return checkNodes;
    }
    function render_grid_inheriRoles() {
        grid_inheriRoles = $("#ligergrid_inheritRoles").ligerGrid({
        	url: '<%=path%>/system/sysRole/initList.do',
            method : 'get',
            delayLoad:true,
            root:'rows',
            record:'total',
            fixedCellHeight: false,
            checkbox: true,
            // frozenCheckbox: false,
            isSingleCheck: true,
            allowHideColumn: false,
            rownumbers:true,
            columns: [ {
                display: '角色名称',
                name: 'roleNm',
                minWidth: 120,
                render: function (rowdata, rowindex, value){
            		return UIUtils.showColumnTip(value);
                }
            }, {
                display: '状态',
                name: 'isValidNm',
                minWidth: 60,
                render: function (rowdata, rowindex, value) {
                	if(rowdata.isValid == CommonConstant.C_YN_Y_1){
                		return LogUtils.LOG_CMM_GRID_BTN_ENABLE;
                	}else if(rowdata.isValid == CommonConstant.C_YN_N_0){
                		return LogUtils.LOG_CMM_GRID_BTN_DISABLE;
                	}
                }
            }, {
                display: '备注',
                name: 'roleRem',
                minWidth: 150,
                render: function (rowdata, rowindex, value){
            		return UIUtils.showColumnTip(value);
                }
            }],
            width: '100%',
            isScroll: false,
            usePagerOnLessPagesize: false,
            pageSizeOptions: [5,10],
            onSelectRow: function (rowdata, rowid, rowobj) {
                //alert("id:"+rowdata.id);
            },
            onAfterShowData: function() {
                SetIframe_height();
            }
        });
    }
    function searchResource (parm) {
        $.ajax({
        	url: '<%=path%>/system/sysRoleResR/getByResNm.do',
            type: 'GET',
            dataType: 'json',
            data: parm,
            success: function (result) {
                tree.setData(result.data.data);
            }
        });
    }
    function resetResource (roleId) {
      
        $.ajax({
        	url: '<%=path%>/system/sysRoleResR/getByRoleId.do?roleId='+roleId,
            type: 'GET',
            dataType: 'json',
            success: function (result) {
                tree.setData(result.data.data);
                RESOURCE_IDs=result.data.IDs;
                //console.log(RESOURCE_IDs);
                //alert(RESOURCE_IDs);
            }
        });
    }
    function setNanoScroller () {
        $(".nanoWrap .nano").nanoScroller({
            preventPageScrolling: true
        });
    }
    
    render_grid_inheriRoles();
});

function discardRole (event,id,isSysDef, gridID) {
	if(isSysDef==1){
		top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_INSIDE_FORBID_HANDLE,SQ_type: "warning"});
		return;
	}; 
    event.stopPropagation();
    
    $.ligerDialog.confirm(LogUtils.LOG_CMM_DISABLE_CONFIRM, function (flag) {
    	if(flag == false)return;
    	$.ajax({
    		type: 'post',
    		dataType:'json',
    		data:{id:id},
    		url: '<%=path%>/system/sysRole/disable.do',
    		success:function(msg){
    			 if(msg.success == 1){
    				 //$.ligerDialog.success(msg.info);
    				 grid.reload();
    			 }else{
    				 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
    			 }
    		}
    	});
    });
    grid.reload();
}
function enableRole (event,id,isSysDef, gridID) {
	if(isSysDef==1){
		top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_INSIDE_FORBID_HANDLE,SQ_type: "warning"});
		return;
	}; 
    event.stopPropagation();
    //alert("enableRole"+ "\n" +"ID:"+id);

    $.ligerDialog.confirm(LogUtils.LOG_CMM_ENABLE_CONFIRM, function (flag) {
    	if(flag == false)return;
    	$.ajax({
    		type: 'post',
    		dataType:'json',
    		data:{id:id},
    		url: '<%=path%>/system/sysRole/enable.do',
    		success:function(msg){
    			 if(msg.success == 1){
    				 grid.reload();
    			 }else{
    				 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
    			 }
    		}
    	});
    });
   
}



$("#addBtn").click(function(event) {
	if(liger.get("addUsr_form")){
		var form=liger.get("addUsr_form");
		$("#resetBtn").click();  //重置 input-text
        form.setData({  //重置表单
            "roleTypCd": " "
        });
	}
    $.ligerDialog.open({
        width: 450,
        // top: 100,
        title : '新增角色',
        target: $("#addUsr_Html")
    });
    $("#saveNewUsr_Btn").show();
	$("#changeSave_Btn").hide();
    var ligerForm = $("#addUsr_form").ligerForm({validate: true});
});
$("#saveNewUsr_Btn").click(function(event) {
    var form = liger.get("addUsr_form");
    if (!form.valid()) {
        top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_ROLE_NAME_NULL,SQ_type: "warning"});
        return false; //验证不通过，不执行以下代码
    }
    var data = form.getData();
    //alert("--- 验证通过！可以提交 ---\n" + JSON.stringify(data));
    $.ligerDialog.hide();
    var waitingDialog=null;

     $.ajax({
         url: '<%=path%>/system/sysRole/add.do',
         type: 'post',
         dataType: 'json',
         data: data,
        beforeSend: function (argument) {
             manager = $.ligerDialog.waitting(LogUtils.LOG_CMM_DATA_LOADING);
        },
         success: function (msg) {
            manager.close();
            if(msg.success == 1){
				 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
				 grid.reload();
			 }else{
				 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
			 }
         }
     });
  	 /*  setTimeout(function () {
            waitingDialog.close();
            $.ligerDialog.success("提交成功！")
        }, 2000); */
});

$("#changeBtn").click(function(event) {
	if(Editable==false){
		top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_INSIDE_FORBID_HANDLE,SQ_type: "warning"});
		return;
	}; 
	var selectedRows = grid.getSelectedRows();
	if(selectedRows.length != 1){
		top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW,SQ_type: "warning"});
		return;
	}
	
	var _id = selectedRows[0].id;
	$("#changeSave_Btn").show();
	$("#saveNewUsr_Btn").hide();
    $.ligerDialog.open({
        width: 500,
        // top: 100,
        title : '修改人员',
        target: $("#addUsr_Html")
    });
  //初始化数据
    initData(_id);
    var ligerForm = $("#addUsr_form").ligerForm({validate: true});
});
$("#changeSave_Btn").click(function(event) {
    var form = liger.get("addUsr_form");
    if (!form.valid()) {
        top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO,SQ_type: "warning"});
        return false; //验证不通过，不执行以下代码
    }
    var data = form.getData();
    //alert("--- 验证通过！可以提交 ---\n" + JSON.stringify(data));
    $.ligerDialog.hide();
    var waitingDialog=null;
    var selectedRows = grid.getSelectedRows();
    var _id = selectedRows[0].id;
    data["id"] = _id;
     $.ajax({
         url: '<%=path%>/system/sysRole/modify.do',
         type: 'post',
         dataType: 'json',
         data: data,
        beforeSend: function (msg) {
             manager = $.ligerDialog.waitting(LogUtils.LOG_CMM_DATA_LOADING);
        },
         success: function (msg) {
            manager.close();
            if(msg.success == 1){
            	top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
				 grid.reload();
			 }else{
				 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
			 }
         }
     });
  	 /*  setTimeout(function () {
            waitingDialog.close();
            $.ligerDialog.success("提交成功！")
        }, 2000); */
});

function initData(_id){
	//alert(_id);
	if(!_id)return;
	var _url = '<%=path%>/system/sysRole/getById.do?id='+_id;
	$.ajax({
		type: 'get',
		dataType:'json',
		data:{id:_id},
		url: _url,
		success:function(msg){
			 if(msg.success == 1){
				 setData(msg.data);//设置数据
			 }else{
				 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
			 }
		}
	});
}

//设置页面数据
function setData(data){
	var form = new liger.get("addUsr_form");
	form.setData({
		roleTypCd:data.roleTypCd,
	});
	$("#roleRem").val(data.roleRem);
	$("#idS").val(data.id);
	$("#roleNm").val(data.roleNm);
	$("#isVis").val(data.isVis);
}

/*
是否系统内置参数获取
*/
function isEditable(isSysDef){
	if(isSysDef==0){
		Editable=true;
	}else{
		Editable=false;
	}
}
</script>

</body>
</html>
